﻿
using UdonSharp;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using VRC.SDK3.Rendering;

[RequireComponent(typeof(Camera))]
public class CameraMonster : UdonSharpBehaviour
{
    [SerializeField] Collider theCollider;
    [SerializeField] private PostProcessVolume postProcessVolume;
    [SerializeField] [Range(0f,5f)]private float fadespeed;
    private Plane[] _planes;
    private Camera _monstercamera;
    void Start()
    {
        _monstercamera = GetComponent<Camera>();
        RefreshCameraSettings();
    }
    //检测玩家更改视野更新视锥体
    public override void OnVRCCameraSettingsChanged(VRCCameraSettings cameraSettings)
    {
        RefreshCameraSettings();
    }
    private void RefreshCameraSettings()
    {
        //客户端运行时生效
        _monstercamera.fieldOfView = VRCCameraSettings.ScreenCamera.FieldOfView - 10f;
    }
    void Update()
    {
        _monstercamera.transform.position = VRCCameraSettings.ScreenCamera.Position;
        _monstercamera.transform.rotation = VRCCameraSettings.ScreenCamera.Rotation;
        _planes = GeometryUtility.CalculateFrustumPlanes(_monstercamera);
        postProcessVolume.weight = GeometryUtility.TestPlanesAABB(_planes,theCollider.bounds) ? 
            Mathf.Clamp(postProcessVolume.weight += fadespeed * Time.deltaTime,0f,1f) : 
            Mathf.Clamp(postProcessVolume.weight -= fadespeed * Time.deltaTime,0f,1f);
    }
}
